package com.hw.weathershow.factory.observer;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.hw.weathershow.entity.Weather;
import com.hw.weathershow.factory.AbstractFactory;
import com.hw.weathershow.factory.ProduceFactory;
import com.hw.weathershow.mapper.WeatherMapper;
import com.hw.weathershow.observer.Observer;
import com.hw.weathershow.observer.WeatherObserver;
import com.hw.weathershow.websocket.UpdateWeatherSocket;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2022/12/6 15:41
 */
@Getter
@Component
public class ObserverFactory implements AbstractFactory {
    @Autowired
    WeatherMapper weatherMapper;
    @Autowired
    UpdateWeatherSocket updateWeatherSocket;

    @Override
    public Observer getObserver(String observer) {
        if(observer.equals("WeatherObserver")) {
            Weather weather = null;
            List<Weather> weathers = weatherMapper.selectList(null);
            if(weathers.size()<=0){
                return null;
            }else {
                weather = weathers.get(0);
            }
            WeatherObserver weatherObserver = new WeatherObserver(updateWeatherSocket,weather);
            return weatherObserver;
        }
        return null;
    }
}
